package me.hys.action.system;

import java.util.Map;

import me.hys.base.AjaxResult;
import me.hys.base.BaseAction;
import me.hys.base.PageQuery;
import me.hys.entity.system.UserEntity;
import me.hys.service.system.IUserManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-12 下午4:20:31 . <br>
 * Function: 系统设置主页
 */
@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction {
	@Autowired
	private IUserManager userManager;

	public UserAction() {
		setFlag(getClass().getSimpleName());
	}

	/**
	 * @Title : edit
	 * @Description : 用户信息修改页面
	 */
	@RequestMapping("/edit/{userid}")
	public ModelAndView edit(@PathVariable String userid) {
		Map<String, Object> data = getMap();
		data.put("user", userManager.findById(userid));
		return new ModelAndView("/system/user_form", data);
	}

	/**
	 * @Title : index
	 * @Description : 用户管理首页
	 */
	@RequestMapping("/index")
	public ModelAndView index() {
		Map<String, Object> data = getMap();
		PageQuery<UserEntity> pageQuery = getPageQuery();
		data.put("users", userManager.findPage(pageQuery));
		return new ModelAndView("/system/user", data);
	}

	/**
	 * @Title : save
	 * @Description : 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(@ModelAttribute UserEntity entity,
			@RequestHeader(value = "referer") String url) {
		userManager.saveOrUpdate(entity);
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult.setMsg(url);
		return ajaxResult;
	}
}
